/**
 * @file task_test.c
 * @brief 
 * @author Tome (baojian.liu-a1105@aqara.com)
 * @date 2021-01-05
 */
 

#include "OSAL.h" 
#include "osal_log.h"

#include "hal_led.h"
#include "hal_key.h"

#include "stdio.h"

#include "task_test.h"
#include "task_time.h"

#include "onboard.h"

#include "tlv_tree.h"

 
/****************************************************************************************************************************** 
                                                            const
*******************************************************************************************************************************/












/****************************************************************************************************************************** 
                                                            typedef
*******************************************************************************************************************************/













/****************************************************************************************************************************** 
                                                            MICRO
*******************************************************************************************************************************/


#define TASK_TEST_ENTRY                     Test_task_num //10
#define TASK_TEST_SEND_MSG_INTERNAL_MIN     10 //ms



/****************************************************************************************************************************** 
                                                            declearation
*******************************************************************************************************************************/

extern void test_pin_toggle(void);


static void task_test_ProcessOSALMsg(osal_event_hdr_t *pMsg );

/****************************************************************************************************************************** 
                                                            defination
*******************************************************************************************************************************/



static uint8 Test_TaskID;
static uint8 Test_task_num = 0;
static uint8 Test_task_min = 0;




/**************************************************************************************************
 * @fn      Hal_Init
 *
 * @brief   Hal Initialization function.
 *
 * @param   task_id - Hal TaskId
 *
 * @return  None
 **************************************************************************************************/
void Test_Init( uint8 task_id )
{
    
  static uint8 init = FALSE;    
  /* Register task ID */
  Test_TaskID = task_id;
    
  UNUSED_VARIABLE(Test_TaskID);
    
    
  if ( init == FALSE )
  {
      
      Test_task_min = task_id;
      
//     osal_start_reload_timer(task_id, TASK_TEST_EVT_1S, 1000 );
//     osal_start_timerEx(task_id, TASK_TEST_EVT_RDMSEND, 500 );
//     HalLedBlink(HAL_LED_4, 10, 50, 1000);
//      
      
    osal_start_timerEx(task_id, TASK_TEST_EVT_PM, 300);
    //osal_pwrmgr_task_state( task_id, PWRMGR_HOLD);
      
      
    RegisterForKeys(task_id);  
      
    init = TRUE;
  }
    
  LOG("test task %d start", task_id);
  
  if ( Test_task_num < task_id )Test_task_num = task_id;
  
}



/*   change system power mode         */
void test_task_powermode_change(void)
{
    static uint8  sleep = 0;
    if ( sleep == 0)
    {
        osal_pwrmgr_task_state( Test_task_min, PWRMGR_HOLD);
        HalLedSet(HAL_LED_4, HAL_LED_MODE_ON);
        
    }
    else
    {
        osal_pwrmgr_task_state( Test_task_min, PWRMGR_CONSERVE);
        HalLedSet(HAL_LED_4, HAL_LED_MODE_OFF);
    }
    
    LOG("system sleep allow %s", sleep ? "YES":"NO");
    
    sleep = !sleep;
    
}


/**************************************************************************************************
 * @fn      Test_ProcessEvent
 *
 * @brief   Test Process Event
 *
 * @param   task_id - Hal TaskId
 *          events - events
 *
 * @return  None
 **************************************************************************************************/
uint16 Test_ProcessEvent( uint8 task_id, uint16 events )
{
  uint8 *msgPtr;
    
  //uint16 rdm = 0;
  //uint8  tar_task;
  
  (void)task_id;  // Intentionally unreferenced parameter

  if ( events & SYS_EVENT_MSG )
  {
        msgPtr = osal_msg_receive(task_id);

        while (msgPtr)
        {
          /* Do something here - for now, just deallocate the msg and move on */
            
          //LOG("T %02d, %s", task_id,msgPtr);
            
           task_test_ProcessOSALMsg((osal_event_hdr_t *)msgPtr );     

          /* De-allocate */
          osal_msg_deallocate( msgPtr );
          /* Next */
          msgPtr = osal_msg_receive( task_id );
        }
      return (events ^ SYS_EVENT_MSG );
  }
  
  if ( events & TASK_TEST_EVT_PM )
  {
      
      test_task_powermode_change();
      
      return ( events ^ TASK_TEST_EVT_PM );
  }
  
  #if 0
  if ( events & TASK_TEST_EVT_RDMSEND )
  {
      msgPtr = osal_msg_allocate(10);
      if ( msgPtr )
      {
          sprintf((char*)msgPtr,"%02d:%02d:%02d", gTime.hour, gTime.min, gTime.sec);
      }
      
      
      do {   
          rdm = osal_rand();
          tar_task = rdm % TASK_TEST_ENTRY ;
      }while (rdm < Test_task_min ) ;
      
      rdm = osal_rand() &0x1FF;
      if (rdm < TASK_TEST_SEND_MSG_INTERNAL_MIN ) rdm += TASK_TEST_SEND_MSG_INTERNAL_MIN;
      
      osal_start_timerEx(task_id, TASK_TEST_EVT_RDMSEND, rdm );
      
      osal_msg_send(tar_task, msgPtr);
      
      return (events ^ TASK_TEST_EVT_RDMSEND );
  }
  #endif
  

   return 0;
}


/*********************************************************************
 * @fn      funcname
 *
 * @brief   
 *
 * @param   
 *
 * @param   
 *
 * @param   
 *
 * @return  
 */


/*********************************************************************
 * @fn      task_test_HandleKeys
 *
 * @brief   Handles all key events for this device.
 *
 * @param   shift - true if in shift/alt.
 * @param   keys  - bit field for key events.
 *
 * @return  none
 */
void task_test_HandleKeys( uint8 shift, uint8 keys )
{
   
    UNUSED_VARIABLE(shift);
    
    
    
    if ( keys  & HAL_BTN_DK )
    {
        LOG("key press");
        //test_task_powermode_change();
        tlv_and_tree_test();
    }
    
   // test_pin_toggle();
    

        
    //HalLedBreath(HAL_LED_4, 100, 50, 1000);
    
}
 
/******************************************************************************
* @fn        task_test_ProcessOSALMsg
*
* @brief     osal system events handler
*
* @param     
*
* @return    none
*/
static void task_test_ProcessOSALMsg(osal_event_hdr_t *pMsg )
{
switch ( pMsg->event )
  {
  case UART_EVENT:
    //task_test_HandleUarts ( ((osalUartEvt_t*)pMsg)->event );
    
    break;

    
  case KEY_CHANGE:
    task_test_HandleKeys(  ((keyChange_t*)pMsg)->state, ((keyChange_t*)pMsg)->keys );
    break;
    
    
  default:
    break;
  }  
}



/*********************************************************************
 * @fn      funcname
 *
 * @brief   
 *
 * @param   
 *
 * @param   
 *
 * @param   
 *
 * @return  
 */


/*********************************************************************
 * @fn      funcname
 *
 * @brief   
 *
 * @param   
 *
 * @param   
 *
 * @param   
 *
 * @return  
 */


/*********************************************************************
 * @fn      funcname
 *
 * @brief   
 *
 * @param   
 *
 * @param   
 *
 * @param   
 *
 * @return  
 */


/*********************************************************************
 * @fn      funcname
 *
 * @brief   
 *
 * @param   
 *
 * @param   
 *
 * @param   
 *
 * @return  
 */ 





